/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RadioProp1546;

/**
 *
 * @author Alwyn
 */
public class MathsHelper {

    public static double invNormalDist(double x)
    {
        if(x<0.000001)
            x=0.000001;

        if(x>0.999999)
            x=0.999999;

        if(x<=0.5)
            return (T(x)-eps(x));
        else
            return (eps(1-x)-T(1-x));
    }

    private static double T(double x)
    {
        return Math.sqrt(-2*Math.log(x));
    }

    private static double eps(double x)
    {
        double num=(0.010328*x+0.802853)*x+2.515516698;
        double den=((0.001308*x+0.189269)*x+1.432788)*x+1;

        return num/den;
    }

    public static double logInterpolate(double x, double Xlow, double Xhigh, double Ylow, double Yhigh)
    {
        return Ylow+(Yhigh-Ylow)*Math.log10(x/Xlow)/Math.log10(Xhigh/Xlow);
    }
    
    public static double QInterpolate(double t, double tlow, double thigh, double Elow, double Ehigh)
    {
        t/=100.0;
        thigh/=100.0;
        tlow/=100.0;
        
        return (Ehigh*(tlow-t)/(tlow-thigh))+Elow*(t-thigh)/(tlow-thigh);
    }

    public static double bilinearInterpolate(double x1, double x2,
            double x1l, double x1u, double x2l, double x2u,
            double y1l, double y1u, double y2l, double y2u)
    {
        double t=(x1-x1l)/(x1u-x1l);
        double u=(x2-x2l)/(x2u-x2l);

        return (1-t)*(1-u)*y1l+t*(1-u)*y2l+t*u*y2u+(1-t)*u*y1u;
    }
}
